package com.next.muscu.visual;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.next.muscu.Application;
import com.next.muscu.UserMessageVector;

public class VisualStatusBar extends JPanel implements Observer {

	JTextField mess = new JTextField();
	JButton Btdeploy = null;
	JList messages = new JList();
	DefaultListModel model = new DefaultListModel (); 
	
	VisualStatusBar()
	{
		Icon z = Application.getAppl().getImage(Application.IMG_COMMENT_ADD);
		Btdeploy = new JButton(z);
		Btdeploy.addActionListener(new btOpenListener());
		BoxLayout a = new BoxLayout(this, BoxLayout.Y_AXIS);
		messages.setModel(model);
		this.setLayout(a);
		mess.setEditable(false);
		JPanel panel1 = new JPanel();
		panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
		panel1.add(mess);
		panel1.add(Btdeploy);
		this.add(panel1);
		this.add(messages);
		messages.setVisible(false);
		Application.getAppl().getUserMessages().addObserver(this);
	}
	
	private void Notify(String message)
	{
		mess.setText(message);
		model.addElement(message);
	}
	
	class btOpenListener extends AbstractAction
	{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(messages.isVisible())
			{
				messages.setVisible(false);
				Btdeploy.setIcon(Application.getAppl().getImage(Application.IMG_COMMENT_ADD));
			}
			else
			{
				messages.setVisible(true);
				Btdeploy.setIcon(Application.getAppl().getImage(Application.IMG_COMMENT));
			}
		}
		
		
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		if(arg0 instanceof UserMessageVector)
		{
			UserMessageVector v = (UserMessageVector) arg0;
			Notify(v.getMessage(v.size()-1));
			
		}
		
	}

}
